#!/bin/bash
#
##############################################################################
#                                                                            #
#                        Pi-Star Auto Upgrade Tool                           #
#                                                                            #
#     Version 1.1, Code, Design and Development by Andy Taylor (MW0MWZ).     #
#                                                                            #
#        Make it simple to upgrade to the current version of Pi-Star.        #
#                                                                            #
##############################################################################
#
if [ "$(id -u)" != "0" ]; then
  echo -e "You need to be root to run this command...\n"
  exit 1
fi

exec 200>/var/lock/pistar-upgrade.lock || exit 1
if ! flock -n 200 ; then
  echo -e "Another instance is already running...\n"
  exit 1
fi

main_function() {
	# Get the current Modem
	if [ -f /etc/dstar-radio.mmdvmhost ]; then
		pistarModem=$(awk -F "=" '/Hardware/ {print $2}' /etc/dstar-radio.mmdvmhost)
	elif [ -f /etc/dstar-radio.dstarrepeater ]; then
		pistarModem=$(awk -F "=" '/Hardware/ {print $2}' /etc/dstar-radio.dstarrepeater)
	else
		pistarModem=Unknown
	fi

	# Get the Pi-Star Version Info
	pistarCurVersion=$(awk -F "= " '/Version/ {print $2}' /etc/pistar-release)
	# Get the hardware type, this may be important later (RPi | NanoPi | OdroidXU4)
	pistarHardware=$(awk -F "= " '/Hardware/ {print $2}' /etc/pistar-release)

	# Make the disk writable
	mount -o remount,rw /
	mount -o remount,rw /boot

	echo "Detected Pi-Star ${pistarCurVersion} running on ${pistarHardware} hardware, attached to ${pistarModem} modem..."

	case ${pistarCurVersion} in
	3.4.1)
		if [ ! -z "${pistarHardware}" ]; then
			Message="Upgraded from ${pistarCurVersion} to 3.4.2..."
			echo "Installing extra software..."
			apt-get update
			apt-get install -y stm32flash
			echo "Cleaning up unused files..."
	                rm -f /usr/local/etc/config_clean.zip
			echo "Bumping the version information..."
        	        sed -i 's/3.4.1/3.4.2/g' /etc/pistar-release
                	sed -i 's/3.4.1/3.4.2/g' /etc/motd
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.1 are not supported..."
		fi
	;;
	3.4.2)
		if [ ! -z "${pistarHardware}" ]; then
			Message="Upgraded from ${pistarCurVersion} to 3.4.3..."
			sed -i 's/3.4.2/3.4.3/g' /etc/pistar-release
			sed -i 's/3.4.2/3.4.3/g' /etc/motd
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.2 are not supported..."
		fi
	;;
	3.4.3)
		# Check to see if the USB Max Current setting is in place or not...
		if grep -Fxq "max_usb_current=1" /boot/config.txt
			then
    			# It's already there, just version bump
			if [ ! -z "${pistarHardware}" ]; then
				Message="Upgraded from ${pistarCurVersion} to 3.4.4..."
				sed -i 's/3.4.3/3.4.4/g' /etc/pistar-release
				sed -i 's/3.4.3/3.4.4/g' /etc/motd
				echo $Message
				exec "$0" "$@"
                        	exit 1
			else
				Message="In-place upgrades from 3.4.3 are not supported..."
			fi
		else
			# Add the config lines and then version bump
			echo "Updating boot config to add high current USB support"
			echo -e "\n# Increase USB Current\nmax_usb_current=1" >> /boot/config.txt
			if [ ! -z "${pistarHardware}" ]; then
				Message="Upgraded from ${pistarCurVersion} to 3.4.4..."
				sed -i 's/3.4.3/3.4.4/g' /etc/pistar-release
				sed -i 's/3.4.3/3.4.4/g' /etc/motd
				echo $Message
				exec "$0" "$@"
                        	exit 1
			else
				Message="In-place upgrades from 3.4.3 are not supported..."
			fi
		fi
	;;
	3.4.4)
		if [ ! -z "${pistarHardware}" ]; then
			Message="Upgraded from ${pistarCurVersion} to 3.4.5..."
			sed -i 's/3.4.4/3.4.5/g' /etc/pistar-release
			sed -i 's/3.4.4/3.4.5/g' /etc/motd
			echo "Installing Shell in a Box..."
			apt-get update
			apt-get install -y shellinabox
			echo "Configuring Shell in a Box..."
			sed -i '/SHELLINABOX_PORT=/c SHELLINABOX_PORT=2222' /etc/default/shellinabox
			sed -i '/SHELLINABOX_ARGS=/c SHELLINABOX_ARGS="--no-beep --disable-ssl-menu --disable-ssl --css=/etc/shellinabox/options-enabled/00_White\\ On\\ Black.css"' /etc/default/shellinabox
			rm -rf /etc/shellinabox/options-enabled/00+Black\ on\ White.css
			rm -rf /etc/shellinabox/options-enabled/01_Monochrome.css
			systemctl enable shellinabox
			systemctl start shellinabox
			systemctl restart shellinabox
			echo "Updating firewall..."
			/usr/local/sbin/pistar-firewall  > /dev/null 2>&1
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.4 are not supported..."
		fi
	;;
	3.4.5)
		if [ ! -z "${pistarHardware}" ]; then
			sed -i 's/3.4.5/3.4.6/g' /etc/pistar-release
                	sed -i 's/3.4.5/3.4.6/g' /etc/motd
			sed -i '13d;17d' /etc/motd
			sed -i '2,9d' /etc/motd
			sed -i '2i║                                                                       ║' /etc/motd
			sed -i '3i║           ██████╗ ██╗      ███████╗████████╗ █████╗ ██████╗           ║' /etc/motd
			sed -i '4i║           ██╔══██╗██║      ██╔════╝╚══██╔══╝██╔══██╗██╔══██╗          ║' /etc/motd
			sed -i '5i║           ██████╔╝██║█████╗███████╗   ██║   ███████║██████╔╝          ║' /etc/motd
			sed -i '6i║           ██╔═══╝ ██║╚════╝╚════██║   ██║   ██╔══██║██╔══██╗          ║' /etc/motd
			sed -i '7i║           ██║     ██║      ███████║   ██║   ██║  ██║██║  ██║          ║' /etc/motd
			sed -i '8i║           ╚═╝     ╚═╝      ╚══════╝   ╚═╝   ╚═╝  ╚═╝╚═╝  ╚═╝          ║' /etc/motd
			sed -i '9i║                                                                       ║' /etc/motd
			Message="Upgraded from ${pistarCurVersion} to 3.4.6..."
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.4 are not supported..."
		fi
	;;
	3.4.6)
		if [ ! -z "${pistarHardware}" ]; then
			sed -i 's/3.4.6/3.4.7/g' /etc/pistar-release
                	sed -i 's/3.4.6/3.4.7/g' /etc/motd
			apt-get update
			apt-get install -y libimobiledevice-utils usb-modeswitch usb-modeswitch-data
			Message="Upgraded from ${pistarCurVersion} to 3.4.7..."
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.4 are not supported..."
		fi
	;;
	3.4.7)
		if [ ! -z "${pistarHardware}" ]; then
			sed -i 's/3.4.7/3.4.8/g' /etc/pistar-release
                	sed -i 's/3.4.7/3.4.8/g' /etc/motd
			cd /tmp
			curl --fail -O -s http://archive.raspbian.org/raspbian/pool/main/o/openssl1.0/libssl1.0.2_1.0.2l-2+deb9u3_armhf.deb
			curl --fail -O -s http://archive.raspbian.org/raspbian/pool/main/s/shellinabox/shellinabox_2.20_armhf.deb
			dpkg -i /tmp/libssl1.0.2_1.0.2l-2+deb9u3_armhf.deb
			rm /tmp/libssl1.0.2_1.0.2l-2+deb9u3_armhf.deb
			if [ -z "$STY" ]; then exec screen -dm -S update dpkg -i /tmp/shellinabox_2.20_armhf.deb "$0"; fi
			Message="Upgraded from ${pistarCurVersion} to 3.4.8..."
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.4 are not supported..."
		fi
	;;
	3.4.8)
		if [ ! -z "${pistarHardware}" ]; then
			sed -i 's/3.4.8/3.4.9/g' /etc/pistar-release
			sed -i 's/3.4.8/3.4.9/g' /etc/motd
			apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 7638D0442B90D010
			apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 8B48AD6246925553
			echo 'deb http://httpredir.debian.org/debian jessie-backports main contrib non-free' > /etc/apt/sources.list.d/jessie-backports.list
			apt-get update
			apt-get -t jessie-backports install usb-modeswitch usb-modeswitch-data
			Message="Upgraded from ${pistarCurVersion} to 3.4.9..."
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.4 are not supported..."
		fi
	;;
	3.4.9)
		if [ ! -z "${pistarHardware}" ]; then
			sed -i 's/3.4.9/3.4.10/g' /etc/pistar-release
                	sed -i '10,1d' /etc/motd
			sed -i '10i╚═══════════════════════════════════════════════════════════════v3.4.10═╝' /etc/motd

			# Add the Colby Ross Random Seed fix
			rm -rf /var/lib/systemd/random-seed
			ln -s /tmp/random-seed /var/lib/systemd/random-seed

			# Add the Unit file(s)
			echo '[Unit]' > /lib/systemd/system/ysf2dmr.service
			echo 'Description=Pi-Star YSF2DMR Service' >> /lib/systemd/system/ysf2dmr.service
			echo 'After=syslog.target network.target mmdvmhost.timer' >> /lib/systemd/system/ysf2dmr.service
			echo '' >> /lib/systemd/system/ysf2dmr.service
			echo '[Service]' >> /lib/systemd/system/ysf2dmr.service
			echo 'Type=forking' >> /lib/systemd/system/ysf2dmr.service
			echo 'ExecStart=/usr/local/sbin/ysf2dmr.service start' >> /lib/systemd/system/ysf2dmr.service
			echo 'ExecStop=/usr/local/sbin/ysf2dmr.service stop' >> /lib/systemd/system/ysf2dmr.service
			echo 'ExecReload=/usr/local/sbin/ysf2dmr.service restart' >> /lib/systemd/system/ysf2dmr.service
			echo '' >> /lib/systemd/system/ysf2dmr.service
			echo '[Install]' >> /lib/systemd/system/ysf2dmr.service
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/ysf2dmr.service

			echo '[Timer]' > /lib/systemd/system/ysf2dmr.timer
			echo 'OnStartupSec=20' >> /lib/systemd/system/ysf2dmr.timer
			echo '' >> /lib/systemd/system/ysf2dmr.timer
			echo '[Install]' >> /lib/systemd/system/ysf2dmr.timer
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/ysf2dmr.timer

			# Restart SystemD and add the service
			systemctl daemon-reload
			systemctl enable ysf2dmr.timer

			Message="Upgraded from ${pistarCurVersion} to 3.4.10..."
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.4 are not supported..."
		fi
	;;
	3.4.10)
		if [ ! -z "${pistarHardware}" ]; then
			sed -i 's/3.4.10/3.4.11/g' /etc/pistar-release
                	sed -i 's/3.4.10/3.4.11/g' /etc/motd

			# Install the tools
			apt-get update
			apt-get install -y hostapd dnsmasq
			systemctl daemon-reload
			systemctl disable hostapd
			systemctl disable dnsmasq

			# Setup the config(s)
			echo 'DAEMON_CONF="/etc/hostapd/hostapd.conf"' >> /etc/default/hostapd

			echo 'interface=wlan0_ap' > /etc/hostapd/hostapd.conf
			echo 'ssid=Pi-Star' >> /etc/hostapd/hostapd.conf
			echo 'hw_mode=g' >> /etc/hostapd/hostapd.conf
			echo 'macaddr_acl=0' >> /etc/hostapd/hostapd.conf
			echo 'auth_algs=1' >> /etc/hostapd/hostapd.conf
			echo 'ignore_broadcast_ssid=0' >> /etc/hostapd/hostapd.conf
			echo 'channel=6' >> /etc/hostapd/hostapd.conf
			echo 'wpa=2' >> /etc/hostapd/hostapd.conf
			echo 'wpa_passphrase=raspberry' >> /etc/hostapd/hostapd.conf
			echo 'wpa_key_mgmt=WPA-PSK' >> /etc/hostapd/hostapd.conf
			echo 'wpa_pairwise=TKIP' >> /etc/hostapd/hostapd.conf
			echo 'rsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf

			echo '' >> /etc/network/interfaces
			echo 'iface wlan0_ap inet static' >> /etc/network/interfaces
			echo '  hwaddress ether b8:27:eb:00:00:01' >> /etc/network/interfaces
			echo '  address 192.168.50.1' >> /etc/network/interfaces
			echo '  netmask 255.255.255.0' >> /etc/network/interfaces
			echo '  network 192.168.50.0' >> /etc/network/interfaces
			echo '  broadcast 192.168.50.255' >> /etc/network/interfaces

			echo 'interface=lo,wlan0_ap' > /etc/dnsmasq.conf
			echo 'server=208.67.222.123' >> /etc/dnsmasq.conf
			echo 'server=208.67.220.123' >> /etc/dnsmasq.conf
			echo 'domain-needed' >> /etc/dnsmasq.conf
			echo 'bogus-priv' >> /etc/dnsmasq.conf
			echo 'dhcp-authoritative' >> /etc/dnsmasq.conf
			echo 'dhcp-range=192.168.50.50,192.168.50.99,4h' >> /etc/dnsmasq.conf
			echo 'dhcp-option=1,255.255.255.0' >> /etc/dnsmasq.conf
			echo 'dhcp-option=3,192.168.50.1' >> /etc/dnsmasq.conf
			echo 'dhcp-option=6,192.168.50.1' >> /etc/dnsmasq.conf

			# Add the Unit file(s)
			echo '[Unit]' > /lib/systemd/system/pistar-ap.service
			echo 'Description=Pi-Star HostAPD Service' >> /lib/systemd/system/pistar-ap.service
			echo 'After=syslog.target network.target' >> /lib/systemd/system/pistar-ap.service
			echo '' >> /lib/systemd/system/pistar-ap.service
			echo '[Service]' >> /lib/systemd/system/pistar-ap.service
			echo 'Type=notify' >> /lib/systemd/system/pistar-ap.service
			echo 'RemainAfterExit=yes' >> /lib/systemd/system/pistar-ap.service
			echo 'ExecStart=/usr/local/sbin/pistar-ap.service start' >> /lib/systemd/system/pistar-ap.service
			echo 'ExecStop=/usr/local/sbin/pistar-ap.service stop' >> /lib/systemd/system/pistar-ap.service
			echo 'ExecReload=/usr/local/sbin/pistar-ap.service restart' >> /lib/systemd/system/pistar-ap.service
			echo '' >> /lib/systemd/system/pistar-ap.service
			echo '[Install]' >> /lib/systemd/system/pistar-ap.service
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/pistar-ap.service

			echo '[Timer]' > /lib/systemd/system/pistar-ap.timer
			echo 'OnStartupSec=120' >> /lib/systemd/system/pistar-ap.timer
			echo '' >> /lib/systemd/system/pistar-ap.timer
			echo '[Install]' >> /lib/systemd/system/pistar-ap.timer
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/pistar-ap.timer

			# Restart SystemD and add the service
			systemctl daemon-reload
			systemctl enable pistar-ap.timer

			# Report the update status
			Message="Upgraded from ${pistarCurVersion} to 3.4.11..."
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.4 are not supported..."
		fi
	;;
	3.4.11)
		if [ ! -z "${pistarHardware}" ]; then
			sed -i 's/3.4.11/3.4.12/g' /etc/pistar-release
                	sed -i 's/3.4.11/3.4.12/g' /etc/motd
			# NXDN Gateway Service
			echo '[Unit]' > /lib/systemd/system/nxdngateway.service
			echo 'Description=NXDN Servce' >> /lib/systemd/system/nxdngateway.service
			echo 'After=syslog.target network.target mmdvmhost.timer' >> /lib/systemd/system/nxdngateway.service
			echo '' >> /lib/systemd/system/nxdngateway.service
			echo '[Service]' >> /lib/systemd/system/nxdngateway.service
			echo 'Type=forking' >> /lib/systemd/system/nxdngateway.service
			echo 'ExecStart=/usr/local/sbin/nxdngateway.service start' >> /lib/systemd/system/nxdngateway.service
			echo 'ExecStop=/usr/local/sbin/nxdngateway.service stop' >> /lib/systemd/system/nxdngateway.service
			echo 'ExecReload=/usr/local/sbin/nxdngateway.service restart' >> /lib/systemd/system/nxdngateway.service
			echo '' >> /lib/systemd/system/nxdngateway.service
			echo '[Install]' >> /lib/systemd/system/nxdngateway.service
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/nxdngateway.service
			# NXDN Gateway Timer
			echo '[Timer]' > /lib/systemd/system/nxdngateway.timer
			echo 'OnStartupSec=20' >> /lib/systemd/system/nxdngateway.timer
			echo '' >> /lib/systemd/system/nxdngateway.timer
			echo '[Install]' >> /lib/systemd/system/nxdngateway.timer
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/nxdngateway.timer
			# NXDN Parrot Service
			echo '[Unit]' > /lib/systemd/system/nxdnparrot.service
			echo 'Description=NXDN Parrot' >> /lib/systemd/system/nxdnparrot.service
			echo 'After=syslog.target network.target nxdngateway.timer' >> /lib/systemd/system/nxdnparrot.service
			echo '' >> /lib/systemd/system/nxdnparrot.service
			echo '[Service]' >> /lib/systemd/system/nxdnparrot.service
			echo 'Type=forking' >> /lib/systemd/system/nxdnparrot.service
			echo 'ExecStart=/usr/local/sbin/nxdnparrot.service start' >> /lib/systemd/system/nxdnparrot.service
			echo 'ExecStop=/usr/local/sbin/nxdnparrot.service stop' >> /lib/systemd/system/nxdnparrot.service
			echo 'ExecReload=/usr/local/sbin/nxdnparrot.service restart' >> /lib/systemd/system/nxdnparrot.service
			echo '' >> /lib/systemd/system/nxdnparrot.service
			echo '[Install]' >> /lib/systemd/system/nxdnparrot.service
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/nxdnparrot.service
			# NXDN Parrot Timer
			echo '[Timer]' > /lib/systemd/system/nxdnparrot.timer
			echo 'OnStartupSec=25' >> /lib/systemd/system/nxdnparrot.timer
			echo '' >> /lib/systemd/system/nxdnparrot.timer
			echo '[Install]' >> /lib/systemd/system/nxdnparrot.timer
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/nxdnparrot.timer
			# NXDN Gateway Config
			echo '[General]' > /etc/nxdngateway
			echo 'Callsign=M1ABC' >> /etc/nxdngateway
			echo 'RptAddress=127.0.0.1' >> /etc/nxdngateway
			echo 'RptPort=14021' >> /etc/nxdngateway
			echo 'LocalPort=14020' >> /etc/nxdngateway
			echo 'Debug=0' >> /etc/nxdngateway
			echo 'Daemon=1' >> /etc/nxdngateway
			echo '' >> /etc/nxdngateway
			echo '[Voice]' >> /etc/nxdngateway
			echo 'Enabled=1' >> /etc/nxdngateway
			echo 'Language=en_GB' >> /etc/nxdngateway
			echo 'Directory=/usr/local/etc/NXDN_Audio' >> /etc/nxdngateway
			echo '' >> /etc/nxdngateway
			echo '[Id Lookup]' >> /etc/nxdngateway
			echo 'Name=/usr/local/etc/NXDN.csv' >> /etc/nxdngateway
			echo 'Time=24' >> /etc/nxdngateway
			echo '' >> /etc/nxdngateway
			echo '[Log]' >> /etc/nxdngateway
			echo 'FilePath=/var/log/pi-star' >> /etc/nxdngateway
			echo 'FileRoot=NXDNGateway' >> /etc/nxdngateway
			echo '' >> /etc/nxdngateway
			echo '[Network]' >> /etc/nxdngateway
			echo 'Port=14050' >> /etc/nxdngateway
			echo 'HostsFile=/usr/local/etc/NXDNHosts.txt' >> /etc/nxdngateway
			echo 'ReloadTime=60' >> /etc/nxdngateway
			echo 'ParrotAddress=127.0.0.1' >> /etc/nxdngateway
			echo 'ParrotPort=42021' >> /etc/nxdngateway
			echo 'NXDN2DMRAddress=127.0.0.1' >> /etc/nxdngateway
			echo 'NXDN2DMRPort=42022' >> /etc/nxdngateway
			echo 'Startup=65000' >> /etc/nxdngateway
			echo 'Debug=0' >> /etc/nxdngateway
			# Download the voice files
			mkdir -p /usr/local/etc/NXDN_Audio
			curl --fail -o  /usr/local/etc/NXDN_Audio/de_DE.indx -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/de_DE.indx
			curl --fail -o  /usr/local/etc/NXDN_Audio/de_DE.nxdn -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/de_DE.nxdn
			curl --fail -o  /usr/local/etc/NXDN_Audio/dk_DK.indx -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/dk_DK.indx
			curl --fail -o  /usr/local/etc/NXDN_Audio/dk_DK.nxdn -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/dk_DK.nxdn
			curl --fail -o  /usr/local/etc/NXDN_Audio/en_GB.indx -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/en_GB.indx
			curl --fail -o  /usr/local/etc/NXDN_Audio/en_GB.nxdn -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/en_GB.nxdn
			curl --fail -o  /usr/local/etc/NXDN_Audio/en_US.indx -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/en_US.indx
			curl --fail -o  /usr/local/etc/NXDN_Audio/en_US.nxdn -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/en_US.nxdn
			curl --fail -o  /usr/local/etc/NXDN_Audio/es_ES.indx -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/es_ES.indx
			curl --fail -o  /usr/local/etc/NXDN_Audio/es_ES.nxdn -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/es_ES.nxdn
			curl --fail -o  /usr/local/etc/NXDN_Audio/fr_FR.indx -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/fr_FR.indx
			curl --fail -o  /usr/local/etc/NXDN_Audio/fr_FR.nxdn -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/fr_FR.nxdn
			curl --fail -o  /usr/local/etc/NXDN_Audio/it_IT.indx -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/it_IT.indx
			curl --fail -o  /usr/local/etc/NXDN_Audio/it_IT.nxdn -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/it_IT.nxdn
			curl --fail -o  /usr/local/etc/NXDN_Audio/no_NO.indx -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/no_NO.indx
			curl --fail -o  /usr/local/etc/NXDN_Audio/no_NO.nxdn -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/no_NO.nxdn
			curl --fail -o  /usr/local/etc/NXDN_Audio/pl_PL.indx -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/pl_PL.indx
			curl --fail -o  /usr/local/etc/NXDN_Audio/pl_PL.nxdn -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/pl_PL.nxdn
			curl --fail -o  /usr/local/etc/NXDN_Audio/se_SE.indx -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/se_SE.indx
			curl --fail -o  /usr/local/etc/NXDN_Audio/se_SE.nxdn -s https://raw.githubusercontent.com/g4klx/NXDNClients/master/NXDNGateway/Audio/se_SE.nxdn
			# Update systemd and enable new services
			systemctl daemon-reload
			systemctl enable nxdngateway.timer
			systemctl enable nxdnparrot.timer
			Message="Upgraded from ${pistarCurVersion} to 3.4.12..."
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.4 are not supported..."
		fi
	;;
	3.4.12)
		if [ ! -z "${pistarHardware}" ]; then
			sed -i 's/3.4.12/3.4.13/g' /etc/pistar-release
                	sed -i 's/3.4.12/3.4.13/g' /etc/motd

			# YSF2NXDN Gateway Service
			echo '[Unit]' > /lib/systemd/system/ysf2nxdn.service
			echo 'Description=Pi-Star YSF2NXDN Service' >> /lib/systemd/system/ysf2nxdn.service
			echo 'After=syslog.target network.target mmdvmhost.timer' >> /lib/systemd/system/ysf2nxdn.service
			echo '' >> /lib/systemd/system/ysf2nxdn.service
			echo '[Service]' >> /lib/systemd/system/ysf2nxdn.service
			echo 'Type=forking' >> /lib/systemd/system/ysf2nxdn.service
			echo 'ExecStart=/usr/local/sbin/ysf2nxdn.service start' >> /lib/systemd/system/ysf2nxdn.service
			echo 'ExecStop=/usr/local/sbin/ysf2nxdn.service stop' >> /lib/systemd/system/ysf2nxdn.service
			echo 'ExecReload=/usr/local/sbin/ysf2nxdn.service restart' >> /lib/systemd/system/ysf2nxdn.service
			echo '' >> /lib/systemd/system/ysf2nxdn.service
			echo '[Install]' >> /lib/systemd/system/ysf2nxdn.service
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/ysf2nxdn.service

			# YSF2NXDN Gateway Timer
			echo '[Timer]' > /lib/systemd/system/ysf2nxdn.timer
			echo 'OnStartupSec=20' >> /lib/systemd/system/ysf2nxdn.timer
			echo '' >> /lib/systemd/system/ysf2nxdn.timer
			echo '[Install]' >> /lib/systemd/system/ysf2nxdn.timer
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/ysf2nxdn.timer

			# YSF2P25 Gateway Service
			echo '[Unit]' > /lib/systemd/system/ysf2p25.service
			echo 'Description=Pi-Star YSF2P25 Service' >> /lib/systemd/system/ysf2p25.service
			echo 'After=syslog.target network.target mmdvmhost.timer' >> /lib/systemd/system/ysf2p25.service
			echo '' >> /lib/systemd/system/ysf2p25.service
			echo '[Service]' >> /lib/systemd/system/ysf2p25.service
			echo 'Type=forking' >> /lib/systemd/system/ysf2p25.service
			echo 'ExecStart=/usr/local/sbin/ysf2p25.service start' >> /lib/systemd/system/ysf2p25.service
			echo 'ExecStop=/usr/local/sbin/ysf2p25.service stop' >> /lib/systemd/system/ysf2p25.service
			echo 'ExecReload=/usr/local/sbin/ysf2p25.service restart' >> /lib/systemd/system/ysf2p25.service
			echo '' >> /lib/systemd/system/ysf2p25.service
			echo '[Install]' >> /lib/systemd/system/ysf2p25.service
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/ysf2p25.service

			# YSF2P25 Gateway Timer
			echo '[Timer]' > /lib/systemd/system/ysf2p25.timer
			echo 'OnStartupSec=20' >> /lib/systemd/system/ysf2p25.timer
			echo '' >> /lib/systemd/system/ysf2p25.timer
			echo '[Install]' >> /lib/systemd/system/ysf2p25.timer
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/ysf2p25.timer

			# NXDN Parrot Service
			echo '[Unit]' > /lib/systemd/system/nxdnparrot.service
			echo 'Description=NXDN Parrot' >> /lib/systemd/system/nxdnparrot.service
			echo 'After=syslog.target network.target nxdngateway.timer' >> /lib/systemd/system/nxdnparrot.service
			echo '' >> /lib/systemd/system/nxdnparrot.service
			echo '[Service]' >> /lib/systemd/system/nxdnparrot.service
			echo 'Type=forking' >> /lib/systemd/system/nxdnparrot.service
			echo 'ExecStart=/usr/local/sbin/nxdnparrot.service start' >> /lib/systemd/system/nxdnparrot.service
			echo 'ExecStop=/usr/local/sbin/nxdnparrot.service stop' >> /lib/systemd/system/nxdnparrot.service
			echo 'ExecReload=/usr/local/sbin/nxdnparrot.service restart' >> /lib/systemd/system/nxdnparrot.service
			echo '' >> /lib/systemd/system/nxdnparrot.service
			echo '[Install]' >> /lib/systemd/system/nxdnparrot.service
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/nxdnparrot.service

			# NXDN Parrot Timer
			echo '[Timer]' > /lib/systemd/system/nxdnparrot.timer
			echo 'OnStartupSec=25' >> /lib/systemd/system/nxdnparrot.timer
			echo '' >> /lib/systemd/system/nxdnparrot.timer
			echo '[Install]' >> /lib/systemd/system/nxdnparrot.timer
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/nxdnparrot.timer

			# Download configs
			curl --fail -o /etc/ysf2nxdn -s https://raw.githubusercontent.com/juribeparada/YSF2NXDN/master/YSF2NXDN.ini
			curl --fail -o /etc/ysf2p25 -s https://raw.githubusercontent.com/juribeparada/YSF2P25/master/YSF2P25.ini

			# Update systemd and enable new services
			systemctl daemon-reload
			systemctl enable ysf2nxdn.timer
			systemctl enable ysf2p25.timer

			Message="Upgraded from ${pistarCurVersion} to 3.4.13..."
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.4 are not supported..."
		fi
	;;
	3.4.13)
		if [ ! -z "${pistarHardware}" ]; then
			sed -i 's/3.4.13/3.4.14/g' /etc/pistar-release
        		sed -i 's/3.4.13/3.4.14/g' /etc/motd

			# DMR2YSF Gateway Config
			echo '[YSF Network]' > /etc/dmr2ysf
			echo 'Callsign=M1ABC' >> /etc/dmr2ysf
			echo 'Suffix=ND' >> /etc/dmr2ysf
			echo 'GatewayAddress=127.0.0.1' >> /etc/dmr2ysf
			echo 'GatewayPort=4200' >> /etc/dmr2ysf
			echo 'LocalAddress=127.0.0.1' >> /etc/dmr2ysf
			echo 'LocalPort=3200' >> /etc/dmr2ysf
			echo 'Daemon=1' >> /etc/dmr2ysf
			echo '' >> /etc/dmr2ysf
			echo '[DMR Network]' >> /etc/dmr2ysf
			echo 'Id=1234567' >> /etc/dmr2ysf
			echo 'RptAddress=127.0.0.1' >> /etc/dmr2ysf
			echo 'RptPort=62034' >> /etc/dmr2ysf
			echo 'LocalAddress=127.0.0.1' >> /etc/dmr2ysf
			echo 'LocalPort=62033' >> /etc/dmr2ysf
			echo 'Debug=0' >> /etc/dmr2ysf
			echo '' >> /etc/dmr2ysf
			echo '[DMR Id Lookup]' >> /etc/dmr2ysf
			echo 'File=/usr/local/etc/DMRIds.dat' >> /etc/dmr2ysf
			echo 'Time=24' >> /etc/dmr2ysf
			echo '' >> /etc/dmr2ysf
			echo '[Log]' >> /etc/dmr2ysf
			echo 'DisplayLevel=0' >> /etc/dmr2ysf
			echo 'FileLevel=2' >> /etc/dmr2ysf
			echo 'FilePath=/var/log/pi-star' >> /etc/dmr2ysf
			echo 'FileRoot=DMR2YSF' >> /etc/dmr2ysf
			echo '' >> /etc/dmr2ysf
			echo '[Enabled]' >> /etc/dmr2ysf
			echo 'Enabled=0' >> /etc/dmr2ysf

			# DMR2YSF Gateway Service
			echo '[Unit]' > /lib/systemd/system/dmr2ysf.service
			echo 'Description=Pi-Star DMR2YSF Service' >> /lib/systemd/system/dmr2ysf.service
			echo 'After=syslog.target network.target mmdvmhost.timer' >> /lib/systemd/system/dmr2ysf.service
			echo '' >> /lib/systemd/system/dmr2ysf.service
			echo '[Service]' >> /lib/systemd/system/dmr2ysf.service
			echo 'Type=forking' >> /lib/systemd/system/dmr2ysf.service
			echo 'ExecStart=/usr/local/sbin/dmr2ysf.service start' >> /lib/systemd/system/dmr2ysf.service
			echo 'ExecStop=/usr/local/sbin/dmr2ysf.service stop' >> /lib/systemd/system/dmr2ysf.service
			echo 'ExecReload=/usr/local/sbin/dmr2ysf.service restart' >> /lib/systemd/system/dmr2ysf.service
			echo '' >> /lib/systemd/system/dmr2ysf.service
			echo '[Install]' >> /lib/systemd/system/dmr2ysf.service
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/dmr2ysf.service

			# DMR2YSF Gateway Timer
			echo '[Timer]' > /lib/systemd/system/dmr2ysf.timer
			echo 'OnStartupSec=20' >> /lib/systemd/system/dmr2ysf.timer
			echo '' >> /lib/systemd/system/dmr2ysf.timer
			echo '[Install]' >> /lib/systemd/system/dmr2ysf.timer
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/dmr2ysf.timer

			# Update systemd and enable new services
			systemctl daemon-reload
			systemctl enable dmr2ysf.timer

			Message="Upgraded from ${pistarCurVersion} to 3.4.14..."
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.4 are not supported..."
		fi
	;;
	3.4.14)
		if [ ! -z "${pistarHardware}" ]; then
			sed -i 's/3.4.14/3.4.15/g' /etc/pistar-release
        		sed -i 's/3.4.14/3.4.15/g' /etc/motd

			# DMR2NXDN Gateway Config
			echo '[NXDN Network]' > /etc/dmr2nxdn
			echo 'GatewayAddress=127.0.0.1' >> /etc/dmr2nxdn
			echo 'GatewayPort=14020' >> /etc/dmr2nxdn
			echo 'LocalAddress=127.0.0.1' >> /etc/dmr2nxdn
			echo 'LocalPort=14021' >> /etc/dmr2nxdn
			echo 'Daemon=1' >> /etc/dmr2nxdn
			echo '' >> /etc/dmr2nxdn
			echo '[DMR Network]' >> /etc/dmr2nxdn
			echo 'Id=1234567' >> /etc/dmr2nxdn
			echo 'RptAddress=127.0.0.1' >> /etc/dmr2nxdn
			echo 'RptPort=62036' >> /etc/dmr2nxdn
			echo 'LocalAddress=127.0.0.1' >> /etc/dmr2nxdn
			echo 'LocalPort=62035' >> /etc/dmr2nxdn
			echo 'Debug=0' >> /etc/dmr2nxdn
			echo '' >> /etc/dmr2nxdn
			echo '[DMR Id Lookup]' >> /etc/dmr2nxdn
			echo 'File=/usr/local/etc/DMRIds.dat' >> /etc/dmr2nxdn
			echo 'Time=24' >> /etc/dmr2nxdn
			echo '' >> /etc/dmr2nxdn
			echo '[NXDN Id Lookup]' >> /etc/dmr2nxdn
			echo 'File=/usr/local/etc/NXDN.csv' >> /etc/dmr2nxdn
			echo 'Time=24' >> /etc/dmr2nxdn
			echo '' >> /etc/dmr2nxdn
			echo '[Log]' >> /etc/dmr2nxdn
			echo 'DisplayLevel=0' >> /etc/dmr2nxdn
			echo 'FileLevel=2' >> /etc/dmr2nxdn
			echo 'FilePath=/var/log/pi-star' >> /etc/dmr2nxdn
			echo 'FileRoot=DMR2NXDN' >> /etc/dmr2nxdn
			echo '' >> /etc/dmr2nxdn
			echo '[Enabled]' >> /etc/dmr2nxdn
			echo 'Enabled=0' >> /etc/dmr2nxdn

			# DMR2NXDN Gateway Service
			echo '[Unit]' > /lib/systemd/system/dmr2nxdn.service
			echo 'Description=Pi-Star DMR2NXDN Service' >> /lib/systemd/system/dmr2nxdn.service
			echo 'After=syslog.target network.target mmdvmhost.timer' >> /lib/systemd/system/dmr2nxdn.service
			echo '' >> /lib/systemd/system/dmr2nxdn.service
			echo '[Service]' >> /lib/systemd/system/dmr2nxdn.service
			echo 'Type=forking' >> /lib/systemd/system/dmr2nxdn.service
			echo 'ExecStart=/usr/local/sbin/dmr2nxdn.service start' >> /lib/systemd/system/dmr2nxdn.service
			echo 'ExecStop=/usr/local/sbin/dmr2nxdn.service stop' >> /lib/systemd/system/dmr2nxdn.service
			echo 'ExecReload=/usr/local/sbin/dmr2nxdn.service restart' >> /lib/systemd/system/dmr2nxdn.service
			echo '' >> /lib/systemd/system/dmr2nxdn.service
			echo '[Install]' >> /lib/systemd/system/dmr2nxdn.service
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/dmr2nxdn.service

			# DMR2NXDN Gateway Timer
			echo '[Timer]' > /lib/systemd/system/dmr2nxdn.timer
			echo 'OnStartupSec=20' >> /lib/systemd/system/dmr2nxdn.timer
			echo '' >> /lib/systemd/system/dmr2nxdn.timer
			echo '[Install]' >> /lib/systemd/system/dmr2nxdn.timer
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/dmr2nxdn.timer

			# Update systemd and enable new services
			systemctl daemon-reload
			systemctl enable dmr2nxdn.timer

			Message="Upgraded from ${pistarCurVersion} to 3.4.15..."
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.4 are not supported..."
		fi
	;;
	3.4.15)
		if [ ! -z "${pistarHardware}" ]; then
			sed -i 's/3.4.15/3.4.16/g' /etc/pistar-release
        		sed -i 's/3.4.15/3.4.16/g' /etc/motd

			# DAPNet Gateway Service
			echo '[Unit]' > /lib/systemd/system/dapnetgateway.service
			echo 'Description=Pi-Star DAPNet Gateway Service' >> /lib/systemd/system/dapnetgateway.service
			echo 'After=syslog.target network.target mmdvmhost.timer' >> /lib/systemd/system/dapnetgateway.service
			echo '' >> /lib/systemd/system/dapnetgateway.service
			echo '[Service]' >> /lib/systemd/system/dapnetgateway.service
			echo 'Type=forking' >> /lib/systemd/system/dapnetgateway.service
			echo 'ExecStart=/usr/local/sbin/dapnetgateway.service start' >> /lib/systemd/system/dapnetgateway.service
			echo 'ExecStop=/usr/local/sbin/dapnetgateway.service stop' >> /lib/systemd/system/dapnetgateway.service
			echo 'ExecReload=/usr/local/sbin/dapnetgateway.service restart' >> /lib/systemd/system/dapnetgateway.service
			echo '' >> /lib/systemd/system/dapnetgateway.service
			echo '[Install]' >> /lib/systemd/system/dapnetgateway.service
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/dapnetgateway.service

			# DAPNet Gateway Timer
			echo '[Timer]' > /lib/systemd/system/dapnetgateway.timer
			echo 'OnStartupSec=20' >> /lib/systemd/system/dapnetgateway.timer
			echo '' >> /lib/systemd/system/dapnetgateway.timer
			echo '[Install]' >> /lib/systemd/system/dapnetgateway.timer
			echo 'WantedBy=multi-user.target' >> /lib/systemd/system/dapnetgateway.timer

			# Update systemd and enable new services
			systemctl daemon-reload
			systemctl enable dapnetgateway.timer

			Message="Upgraded from ${pistarCurVersion} to 3.4.16..."
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.4 are not supported..."
		fi
	;;
	3.4.16)
		if [ ! -z "${pistarHardware}" ]; then
			sed -i 's/3.4.16/3.4.17/g' /etc/pistar-release
       			sed -i 's/3.4.16/3.4.17/g' /etc/motd

			# Update all the startup timers
			sed -i "/OnStartupSec=/c\\OnStartupSec=20" /lib/systemd/system/pistar-upnp.timer
			sed -i "/OnStartupSec=/c\\OnStartupSec=20" /lib/systemd/system/dmrgateway.timer
			sed -i "/OnStartupSec=/c\\OnStartupSec=20" /lib/systemd/system/ircddbgateway.timer
			sed -i "/OnStartupSec=/c\\OnStartupSec=20" /lib/systemd/system/nxdnparrot.timer
			sed -i "/OnStartupSec=/c\\OnStartupSec=20" /lib/systemd/system/p25parrot.timer
			sed -i "/OnStartupSec=/c\\OnStartupSec=20" /lib/systemd/system/ysfparrot.timer

			# Update systemd and enable new services
			systemctl daemon-reload

			Message="Upgraded from ${pistarCurVersion} to 3.4.17..."
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.4 are not supported..."
		fi		
	;;
	3.4.17)
		if [ ! -z "${pistarHardware}" ]; then
			sed -i 's/3.4.17/3.4.18/g' /etc/pistar-release
       			sed -i 's/3.4.17/3.4.18/g' /etc/motd
			
			# Fix up the sources to stop some of the update nags
			sed -i 's/mirrordirector/legacy/g' /etc/apt/sources.list
			sed -i 's/httpredir/archive/g' /etc/apt/sources.list.d/jessie-backports.list
			echo 'Acquire::Check-Valid-Until "0";' > /etc/apt/apt.conf.d/10no-check-valid-until
			
			Message="Upgraded from ${pistarCurVersion} to 3.4.18..."
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.4 are not supported..."
		fi
	;;
	3.4.18)
		if [ ! -z "${pistarHardware}" ]; then
			sed -i 's/3.4.18/3.4.19/g' /etc/pistar-release
       			sed -i 's/3.4.18/3.4.19/g' /etc/motd
			
			# Fix up the issues with Lets Encrypt certificate
			sed -i '/^mozilla\/DST_Root_CA_X3.crt$/ s/^/!/' /etc/ca-certificates.conf
			update-ca-certificates
			
			Message="Upgraded from ${pistarCurVersion} to 3.4.19..."
			echo $Message
			exec "$0" "$@"
                        exit 1
		else
			Message="In-place upgrades from 3.4.4 are not supported..."
		fi
	;;
	3.4.19)
		Message="You are already running the latest version..."
	;;
	4.0.0-Beta)
		Message="Please re-image this system with the release version Pi-Star v4.1.x as soon as possible."
	;;
	4.0.0-RC1)
		sed -i 's/4.0.0-RC1/4.0.0-RC2/g' /etc/pistar-release

		# Update all the startup timers
		sed -i "/OnStartupSec=/c\\OnStartupSec=20" /lib/systemd/system/pistar-upnp.timer
		sed -i "/OnStartupSec=/c\\OnStartupSec=20" /lib/systemd/system/dmrgateway.timer
		sed -i "/OnStartupSec=/c\\OnStartupSec=20" /lib/systemd/system/ircddbgateway.timer
		sed -i "/OnStartupSec=/c\\OnStartupSec=20" /lib/systemd/system/nxdnparrot.timer
		sed -i "/OnStartupSec=/c\\OnStartupSec=20" /lib/systemd/system/p25parrot.timer
		sed -i "/OnStartupSec=/c\\OnStartupSec=20" /lib/systemd/system/ysfparrot.timer

		# Update systemd and enable new services
		systemctl daemon-reload

		# Fix some broken libs
		apt-get update && apt-get install --reinstall libraspberrypi0 libraspberrypi-dev libraspberrypi-bin

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen

		Message="Upgraded from ${pistarCurVersion} to 4.0.0-RC2..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.0.0-RC2)
		sed -i 's/4.0.0-RC2/4.0.0-RC3/g' /etc/pistar-release

		# apt-daily.service
		echo '[Unit]' > /etc/systemd/system/apt-daily.service
		echo 'Description=Daily apt download activities' >> /etc/systemd/system/apt-daily.service
		echo 'Documentation=man:apt(8)' >> /etc/systemd/system/apt-daily.service
		echo 'ConditionACPower=true' >> /etc/systemd/system/apt-daily.service
		echo 'After=network-online.target' >> /etc/systemd/system/apt-daily.service
		echo 'Wants=network-online.target' >> /etc/systemd/system/apt-daily.service
		echo '' >> /etc/systemd/system/apt-daily.service
		echo '[Service]' >> /etc/systemd/system/apt-daily.service
		echo 'Type=oneshot' >> /etc/systemd/system/apt-daily.service
		echo 'ExecStartPre=/bin/mount -o remount,rw /' >> /etc/systemd/system/apt-daily.service
		echo 'ExecStartPre=/bin/mount -o remount,rw /boot' >> /etc/systemd/system/apt-daily.service
		echo 'ExecStart=/usr/lib/apt/apt.systemd.daily update' >> /etc/systemd/system/apt-daily.service

		#apt-daily-upgrade.service 
		echo '[Unit]' > /etc/systemd/system/apt-daily-upgrade.service
		echo 'Description=Daily apt upgrade and clean activities' >> /etc/systemd/system/apt-daily-upgrade.service
		echo 'Documentation=man:apt(8)' >> /etc/systemd/system/apt-daily-upgrade.service
		echo 'ConditionACPower=true' >> /etc/systemd/system/apt-daily-upgrade.service
		echo 'After=apt-daily.service' >> /etc/systemd/system/apt-daily-upgrade.service
		echo '' >> /etc/systemd/system/apt-daily-upgrade.service
		echo '[Service]' >> /etc/systemd/system/apt-daily-upgrade.service
		echo 'Type=oneshot' >> /etc/systemd/system/apt-daily-upgrade.service
		echo 'ExecStartPre=/bin/mount -o remount,rw /' >> /etc/systemd/system/apt-daily-upgrade.service
		echo 'ExecStartPre=/bin/mount -o remount,rw /boot' >> /etc/systemd/system/apt-daily-upgrade.service
		echo 'ExecStart=/usr/lib/apt/apt.systemd.daily install' >> /etc/systemd/system/apt-daily-upgrade.service
		echo 'KillMode=process' >> /etc/systemd/system/apt-daily-upgrade.service
		echo 'TimeoutStopSec=900' >> /etc/systemd/system/apt-daily-upgrade.service

		# Update systemd and enable new services
		systemctl daemon-reload

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen

		# OK we are done
		Message="Upgraded from ${pistarCurVersion} to 4.0.0-RC3..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.0.0-RC3)
		sed -i 's/4.0.0-RC3/4.0.0-RC4/g' /etc/pistar-release

		echo '#!/bin/sh' > /usr/local/avrdude
		echo '' >> /usr/local/avrdude
		echo 'sudo strace -o "|autoreset" -eioctl /usr/bin/avrdude-original $@' >> /usr/local/avrdude

		# apt-daily.service
 		echo 'ExecStartPost=/bin/mount -o remount,ro /' >> /etc/systemd/system/apt-daily.service
 		echo 'ExecStartPost=/bin/mount -o remount,ro /boot' >> /etc/systemd/system/apt-daily.service

  		#apt-daily-upgrade.service 
 		echo 'ExecStartPost=/bin/mount -o remount,ro /' >> /etc/systemd/system/apt-daily-upgrade.service
 		echo 'ExecStartPost=/bin/mount -o remount,ro /boot' >> /etc/systemd/system/apt-daily-upgrade.service

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen

		# OK we are done
		Message="Upgraded from ${pistarCurVersion} to 4.0.0-RC4..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.0.0-RC4)
		Message="Please re-image this system with Pi-Star v4.1.x as soon as possible."
	;;
	4.0.0)
		Message="Please re-image this system with Pi-Star v4.1.x as soon as possible."
	;;
	4.1.0-Beta)
		Message="Please re-image this system with the release version Pi-Star v4.1.x as soon as possible."
	;;
	4.1.0-RC1)
		sed -i 's/4.1.0-RC1/4.1.0-RC2/g' /etc/pistar-release

		# Clean up for the new version of Buster going to release
		apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 04EE7237B7D453EC
		apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 648ACFD622F3D138
		apt-get update --allow-releaseinfo-change

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen

		# OK we are done
		Message="Upgraded from ${pistarCurVersion} to 4.1.0-RC2..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.1.0-RC2)
		sed -i 's/4.1.0-RC2/4.1.0-RC3/g' /etc/pistar-release

		# logrotate issues;
		if [[ -f /etc/logrotate.d/alternatives ]]; then
			rm -rf /etc/logrotate.d/alternatives
		fi
		if [[ -f /etc/logrotate.d/btmp ]]; then
			rm -rf /etc/logrotate.d/btmp
		fi
		if [[ -f /etc/logrotate.d/wtmp ]]; then
			rm -rf /etc/logrotate.d/wtmp
		fi
		if [[ -f /etc/logrotate.d/rsyslog.dpkg-dist ]]; then
			rm -rf /etc/logrotate.d/rsyslog.dpkg-dist
		fi
		if [[ $(grep "copytruncate" /etc/logrotate.d/rsyslog | wc -l) -eq 0 ]]; then
			sed -i "s/sharedscripts/sharedscripts\n\tcopytruncate/g" /etc/logrotate.d/rsyslog
		fi
		if [[ $(grep "pgrep" /etc/logrotate.d/rsyslog | wc -l) -eq 0 ]]; then
			sed -i "/invoke-rc.d/c\\\t\t# invoke-rc.d rsyslog rotate > /dev/null\n\t\tkill -s 1 \$(pgrep rsyslog) > /dev/null" /etc/logrotate.d/rsyslog
		fi
		systemctl restart logrotate

		# Add some missing firemwares
		apt-get update && apt-get install -y firmware-misc-nonfree

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen

		# OK we are done
		Message="Upgraded from ${pistarCurVersion} to 4.1.0-RC3..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.1.0-RC3)
		sed -i 's/4.1.0-RC3/4.1.0-RC4/g' /etc/pistar-release

		# Check / Reset the GPIO pins on boot (specifically on the Pi 4 hardware)
		sed -i '/^\/usr\/local\/sbin\/pistar-motdgen/{N;N;s/$/\n# Reset the GPIO Pins on Pi4 Only\n\/usr\/local\/sbin\/pistar-mmdvmhshatreset boot\n/}' /etc/rc.local

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen

		# OK we are done
		Message="Upgraded from ${pistarCurVersion} to 4.1.0-RC4..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.1.0-RC4)
		sed -i 's/4.1.0-RC4/4.1.0-RC5/g' /etc/pistar-release

		# Fix up some minor issues with resolvconf's config
		sed -i '2ifi' /etc/dhcp/dhclient-enter-hooks.d/resolvconf
		sed -i '2i\\ttouch /var/lib/dhcpcd5/resolv.conf' /etc/dhcp/dhclient-enter-hooks.d/resolvconf
		sed -i '2iif [ ! -f "/var/lib/dhcpcd5/resolv.conf" ]; then' /etc/dhcp/dhclient-enter-hooks.d/resolvconf
		sed -i '2i\\n' /etc/dhcp/dhclient-enter-hooks.d/resolvconf

		# Fix fstab to accunt for the change
		sed -i "/\/var\/lib\/dhcpcd5/c\\tmpfs\t\t\t\/var\/lib\/dhcpcd5\ttmpfs\tnodev,noatime,nosuid,mode=1777,size=32k\t\t0\t0" /etc/fstab

		# Change the filesystem to match
		cp /etc/resolv.conf /var/lib/dhcpcd5/resolv.conf
		rm -rf /etc/resolv.conf
		rm -rf /etc/resolv.conf.bak
		ln -s /var/lib/dhcpcd5/resolv.conf /etc/resolv.conf

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen

		# OK we are done
		Message="Upgraded from ${pistarCurVersion} to 4.1.0-RC5..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.1.0-RC5)
		sed -i 's/4.1.0-RC5/4.1.0-RC6/g' /etc/pistar-release

		# Fix issue with sysctl.conf not being applied in Raspbian Buster
		sed -i '3i/etc/init.d/procps restart' /etc/network/if-up.d/pistar-motdgen

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen

		# OK we are done
		Message="Upgraded from ${pistarCurVersion} to 4.1.0-RC6..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.1.0-RC6)
		sed -i 's/4.1.0-RC6/4.1.0-RC7/g' /etc/pistar-release

		# Fix up random seed
		cp /lib/systemd/system/systemd-random-seed.service /etc/systemd/system/systemd-random-seed.service
		sed -i '/ExecStart=\/lib\/systemd\/systemd-random-seed load/a ExecStop=\/bin\/mount -o remount,rw \/' /etc/systemd/system/systemd-random-seed.service
		rm -rf /var/lib/systemd/random-seed

		# Update systemd and enable new services
		systemctl daemon-reload
		systemctl stop systemd-random-seed.service
		systemctl start systemd-random-seed.service

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen

		# OK we are done
		Message="Upgraded from ${pistarCurVersion} to 4.1.0-RC7..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.1.0-RC7)
		sed -i 's/4.1.0-RC7/4.1.0-RC8/g' /etc/pistar-release

		# Fix the missing rng-tools
		apt-get update && apt-get install --reinstall rng-tools

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen

		# OK we are done
		Message="Upgraded from ${pistarCurVersion} to 4.1.0-RC8..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.1.0-RC8)
		sed -i 's/4.1.0-RC8/4.1.0/g' /etc/pistar-release

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen

		# OK we are done
		Message="Upgraded from ${pistarCurVersion} to 4.1.0..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.1.0)
		sed -i 's/4.1.0/4.1.1/g' /etc/pistar-release

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen

		# Fix up issues with missing ralink firmware
		apt-get update && apt-get install --reinstall -y firmware-ralink

		# OK we are done
		Message="Upgraded from ${pistarCurVersion} to 4.1.1..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.1.1)
		sed -i 's/4.1.1/4.1.2/g' /etc/pistar-release

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen

		# Disable the Load/Save RFKill status - it complains on every boot.
		systemctl mask systemd-rfkill.service
		systemctl mask systemd-rfkill.socket
		# Clean up some other broken services
		rm -rf /etc/systemd/system/multi-user.target.wants/exim4.service
		rm -rf /etc/systemd/system/multi-user.target.wants/wifi-country.service
		# Clean out some remaining files
		rm -rf /var/lib/systemd/rfkill/platform-*

		# MobileGPS Gateway Service
		echo '[Unit]' > /lib/systemd/system/mobilegps.service
		echo 'Description=Pi-Star MobileGPS Service' >> /lib/systemd/system/mobilegps.service
		echo 'After=syslog.target network.target' >> /lib/systemd/system/mobilegps.service
		echo '' >> /lib/systemd/system/mobilegps.service
		echo '[Service]' >> /lib/systemd/system/mobilegps.service
		echo 'Type=forking' >> /lib/systemd/system/mobilegps.service
		echo 'ExecStart=/usr/local/sbin/mobilegps.service start' >> /lib/systemd/system/mobilegps.service
		echo 'ExecStop=/usr/local/sbin/mobilegps.service stop' >> /lib/systemd/system/mobilegps.service
		echo 'ExecReload=/usr/local/sbin/mobilegps.service restart' >> /lib/systemd/system/mobilegps.service
		echo '' >> /lib/systemd/system/mobilegps.service
		echo '[Install]' >> /lib/systemd/system/mobilegps.service
		echo 'WantedBy=multi-user.target' >> /lib/systemd/system/mobilegps.service
		
		# Enable the service
		systemctl daemon-reload
		systemctl enable mobilegps.service
		
		# Modify the /boot/config.txt to bring it up to date
		sed -i '/USB Current/d' /boot/config.txt
		sed -i '/max_usb_current/d' /boot/config.txt
		sed -i '/Buster Support/d' /boot/config.txt
		sed -i '/dtoverlay=vc4-fkms-v3d/d' /boot/config.txt
		sed -i 's/pi3-miniuart-bt/miniuart-bt/g' /boot/config.txt
		echo "" >> /boot/config.txt
		echo "[pi4]" >> /boot/config.txt
		echo "# Enable DRM VC4 V3D driver on top of the dispmanx display stack" >> /boot/config.txt
		echo "dtoverlay=vc4-fkms-v3d" >> /boot/config.txt
		echo "max_framebuffers=2" >> /boot/config.txt
		echo "" >> /boot/config.txt
		echo "[all]" >> /boot/config.txt
		echo "#dtoverlay=vc4-fkms-v3d" >> /boot/config.txt

		# Remove the exim4 stuff from fstab
		sed -i '/exim4/d' /etc/fstab

		# Add Captive Portal enhancement
		echo "address=/www.msftconnecttest.com/192.168.50.1" >>/etc/dnsmasq.conf
		echo "address=/www.msftncsi.com/192.168.50.1" >>/etc/dnsmasq.conf
		echo "address=/clients3.google.com/192.168.50.1" >>/etc/dnsmasq.conf
		echo "address=/connectivitycheck.gstatic.com/192.168.50.1" >>/etc/dnsmasq.conf
		echo "address=/connectivitycheck.android.com/192.168.50.1" >>/etc/dnsmasq.conf
		echo "address=/www.appleiphonecell.com/192.168.50.1" >>/etc/dnsmasq.conf
		echo "address=/captive.apple.com/192.168.50.1" >>/etc/dnsmasq.conf
		echo "address=/www.apple.com/192.168.50.1" >>/etc/dnsmasq.conf
		echo "address=/.apple.com.edgekey.net/192.168.50.1" >>/etc/dnsmasq.conf

		# Add the Captive Portal to nginx
		echo "server_names_hash_bucket_size 64;" > /etc/nginx/sites-available/captive-portal
		echo "" >> /etc/nginx/sites-available/captive-portal
		echo "server {" >> /etc/nginx/sites-available/captive-portal
		echo "	listen 80;" >> /etc/nginx/sites-available/captive-portal
		echo "	server_name www.msftconnecttest.com www.msftncsi.com clients3.google.com;" >> /etc/nginx/sites-available/captive-portal
		echo "	return 302 http://192.168.50.1/index.php;" >> /etc/nginx/sites-available/captive-portal
		echo "}" >> /etc/nginx/sites-available/captive-portal
		echo "" >> /etc/nginx/sites-available/captive-portal
		echo "server {" >> /etc/nginx/sites-available/captive-portal
		echo "	listen 80;" >> /etc/nginx/sites-available/captive-portal
		echo "	server_name connectivitycheck.gstatic.com connectivitycheck.android.com;" >> /etc/nginx/sites-available/captive-portal
		echo "	return 302 http://192.168.50.1/index.php;" >> /etc/nginx/sites-available/captive-portal
		echo "}" >> /etc/nginx/sites-available/captive-portal
		echo "" >> /etc/nginx/sites-available/captive-portal
		echo "server {" >> /etc/nginx/sites-available/captive-portal
		echo "	listen 80;" >> /etc/nginx/sites-available/captive-portal
		echo "	server_name www.appleiphonecell.com captive.apple.com www.apple.com;" >> /etc/nginx/sites-available/captive-portal
		echo "	return 302 http://192.168.50.1/index.php;" >> /etc/nginx/sites-available/captive-portal
		echo "}" >> /etc/nginx/sites-available/captive-portal
		echo "" >> /etc/nginx/sites-available/captive-portal
		echo "server {" >> /etc/nginx/sites-available/captive-portal
		echo "	listen 80;" >> /etc/nginx/sites-available/captive-portal
		echo "	server_name *.apple.com.edgekey.net;" >> /etc/nginx/sites-available/captive-portal
		echo "	return 302 http://192.168.50.1/index.php;" >> /etc/nginx/sites-available/captive-portal
		echo "}" >> /etc/nginx/sites-available/captive-portal
		echo "" >> /etc/nginx/sites-available/captive-portal
		echo "server {" >> /etc/nginx/sites-available/captive-portal
		echo "        listen 192.168.50.1:80;" >> /etc/nginx/sites-available/captive-portal
		echo "        root /var/www/dashboard;" >> /etc/nginx/sites-available/captive-portal
		echo "" >> /etc/nginx/sites-available/captive-portal
		echo "	try_files \$uri \$uri/ /index.php;" >> /etc/nginx/sites-available/captive-portal
		echo "" >> /etc/nginx/sites-available/captive-portal
		echo "        location ^~ /admin {" >> /etc/nginx/sites-available/captive-portal
		echo "		deny	192.168.50.1;" >> /etc/nginx/sites-available/captive-portal
		echo "		allow	192.168.50.0/24;" >> /etc/nginx/sites-available/captive-portal
		echo "		deny	all;" >> /etc/nginx/sites-available/captive-portal
		echo "		try_files \$uri \$uri/ =404;" >> /etc/nginx/sites-available/captive-portal
		echo "		client_max_body_size 512K;" >> /etc/nginx/sites-available/captive-portal
		echo "" >> /etc/nginx/sites-available/captive-portal
		echo "	# Load the defaults" >> /etc/nginx/sites-available/captive-portal
		echo "	include             /etc/nginx/default.d/php.conf;" >> /etc/nginx/sites-available/captive-portal
		echo "	}" >> /etc/nginx/sites-available/captive-portal
		echo "" >> /etc/nginx/sites-available/captive-portal
		echo "	location ~ /\.git {" >> /etc/nginx/sites-available/captive-portal
		echo "		deny all;" >> /etc/nginx/sites-available/captive-portal
		echo "	}" >> /etc/nginx/sites-available/captive-portal
		echo "" >> /etc/nginx/sites-available/captive-portal
		echo "	# Load the defaults" >> /etc/nginx/sites-available/captive-portal
		echo "	include             /etc/nginx/default.d/*.conf;" >> /etc/nginx/sites-available/captive-portal
		echo "}" >> /etc/nginx/sites-available/captive-portal

		# Link the new site into site-enabled
		cd /etc/nginx/sites-enabled
		ln -s ../sites-available/captive-portal ./
		cd

		# Restart nginx to complete the setup
		systemctl restart nginx

		# OK we are done
		Message="Upgraded from ${pistarCurVersion} to 4.1.2..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.1.2)
		sed -i 's/4.1.2/4.1.3/g' /etc/pistar-release

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen

		# Randomise Daily Cron on boot
		sed -i '/^\/usr\/local\/sbin\/pistar-motdgen/a \\n# Shuffle the time for the daily crontab jobs' /etc/rc.local
		sed -i '/^# Shuffle the time for the daily crontab jobs/a cronMins=$(shuf -i 0-59 -n 1)' /etc/rc.local
		sed -i '/^cronMins=*/a cronHours=$(shuf -i 3-4 -n1)' /etc/rc.local
		sed -i '/^cronHours=*/a mount -o remount,rw \/\nsed -i "/cron.daily/c ${cronMins} ${cronHours}\t* * *\troot\tmount -o remount,rw / && cd / && run-parts --report /etc/cron.daily" /etc/crontab' /etc/rc.local

		# OK we are done
		Message="Upgraded from ${pistarCurVersion} to 4.1.3..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.1.3)
		sed -i 's/4.1.3/4.1.4/g' /etc/pistar-release

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen

		# APRS Gateway Service
		echo '[Unit]' > /lib/systemd/system/aprsgateway.service
		echo 'Description=Pi-Star APRS Gateway Service' >> /lib/systemd/system/aprsgateway.service
		echo 'After=syslog.target network.target' >> /lib/systemd/system/aprsgateway.service
		echo '' >> /lib/systemd/system/aprsgateway.service
		echo '[Service]' >> /lib/systemd/system/aprsgateway.service
		echo 'Type=forking' >> /lib/systemd/system/aprsgateway.service
		echo 'ExecStart=/usr/local/sbin/aprsgateway.service start' >> /lib/systemd/system/aprsgateway.service
		echo 'ExecStop=/usr/local/sbin/aprsgateway.service stop' >> /lib/systemd/system/aprsgateway.service
		echo 'ExecReload=/usr/local/sbin/aprsgateway.service restart' >> /lib/systemd/system/aprsgateway.service
		echo '' >> /lib/systemd/system/aprsgateway.service
		echo '[Install]' >> /lib/systemd/system/aprsgateway.service
		echo 'WantedBy=multi-user.target' >> /lib/systemd/system/aprsgateway.service

		# APRS Gateway Timer
		echo '[Timer]' > /lib/systemd/system/aprsgateway.timer
		echo 'OnStartupSec=20' >> /lib/systemd/system/aprsgateway.timer
		echo '' >> /lib/systemd/system/aprsgateway.timer
		echo '[Install]' >> /lib/systemd/system/aprsgateway.timer
		echo 'WantedBy=multi-user.target' >> /lib/systemd/system/aprsgateway.timer
		
		# APRS Gateway Config
		echo '[General]' > /etc/aprsgateway
		echo 'Callsign=M1ABC' >> /etc/aprsgateway
		echo 'Debug=0' >> /etc/aprsgateway
		echo 'Daemon=1' >> /etc/aprsgateway
		echo '' >> /etc/aprsgateway
		echo '[APRS-IS]' >> /etc/aprsgateway
		echo 'Server=rotate.aprs2.net' >> /etc/aprsgateway
		echo 'Port=14580' >> /etc/aprsgateway
		echo 'Password=9999' >> /etc/aprsgateway
		echo '' >> /etc/aprsgateway
		echo '[Log]' >> /etc/aprsgateway
		echo 'DisplayLevel=0' >> /etc/aprsgateway
		echo 'FileLevel=1' >> /etc/aprsgateway
		echo 'FilePath=/var/log/pi-star' >> /etc/aprsgateway
		echo 'FileRoot=APRSGateway' >> /etc/aprsgateway
		echo 'FileRotate=1' >> /etc/aprsgateway
		echo '' >> /etc/aprsgateway
		echo '[Network]' >> /etc/aprsgateway
		echo 'Address=127.0.0.1' >> /etc/aprsgateway
		echo 'Port=8673' >> /etc/aprsgateway
		echo '' >> /etc/aprsgateway
		echo '[Enabled]' >> /etc/aprsgateway
		echo 'Enabled=0' >> /etc/aprsgateway

		# Update systemd and enable new services
		systemctl daemon-reload
		systemctl enable aprsgateway.timer

		# OK we are done
		Message="Upgraded from ${pistarCurVersion} to 4.1.4..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.1.4)
		sed -i 's/4.1.4/4.1.5/g' /etc/pistar-release

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen
		
		# Improved pistar-upnp.service unit file
		echo '[Unit]' > /lib/systemd/system/pistar-upnp.service
		echo 'Description=PiStar-UPnP network service' >> /lib/systemd/system/pistar-upnp.service
		echo 'After=network-online.target ircddbgateway.timer' >> /lib/systemd/system/pistar-upnp.service
		echo 'Wants=network-online.target' >> /lib/systemd/system/pistar-upnp.service
		echo '' >> /lib/systemd/system/pistar-upnp.service
		echo '[Service]' >> /lib/systemd/system/pistar-upnp.service
		echo 'Type=oneshot' >> /lib/systemd/system/pistar-upnp.service
		echo 'RemainAfterExit=true' >> /lib/systemd/system/pistar-upnp.service
		echo 'ExecStart=/usr/local/sbin/pistar-upnp.service start' >> /lib/systemd/system/pistar-upnp.service
		echo 'ExecStopPost=/usr/local/sbin/pistar-upnp.service stop' >> /lib/systemd/system/pistar-upnp.service
		echo '' >> /lib/systemd/system/pistar-upnp.service
		echo '[Install]' >> /lib/systemd/system/pistar-upnp.service
		echo 'WantedBy=multi-user.target' >> /lib/systemd/system/pistar-upnp.service
		
		# Update systemd and enable new services
		systemctl daemon-reload
		if [[ $(systemctl | grep pistar-upnp.timer | wc -l) -gt 0 ]]; then
			systemctl stop pistar-upnp.service
			systemctl start pistar-upnp.service
		fi
		
		# OK we are done
		Message="Upgraded from ${pistarCurVersion} to 4.1.5..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.1.5)
		sed -i 's/4.1.5/4.1.6/g' /etc/pistar-release

		# Re-create the MOTD
		/usr/local/sbin/pistar-motdgen

		# Create DGIdGateway Unit files
		echo '[Unit]' > /lib/systemd/system/dgidgateway.service
		echo 'Description=Yaesu System Fusion DGIdGateway Servce' >> /lib/systemd/system/dgidgateway.service
		echo 'After=syslog.target network.target mmdvmhost.timer' >> /lib/systemd/system/dgidgateway.service
		echo '' >> /lib/systemd/system/dgidgateway.service
		echo '[Service]' >> /lib/systemd/system/dgidgateway.service
		echo 'Type=forking' >> /lib/systemd/system/dgidgateway.service
		echo 'ExecStart=/usr/local/sbin/dgidgateway.service start' >> /lib/systemd/system/dgidgateway.service
		echo 'ExecStop=/usr/local/sbin/dgidgateway.service stop' >> /lib/systemd/system/dgidgateway.service
		echo 'ExecReload=/usr/local/sbin/dgidgateway.service restart' >> /lib/systemd/system/dgidgateway.service
		echo '' >> /lib/systemd/system/dgidgateway.service
		echo '[Install]' >> /lib/systemd/system/dgidgateway.service
		echo 'WantedBy=multi-user.target' >> /lib/systemd/system/dgidgateway.service

		echo '[Timer]' > /lib/systemd/system/dgidgateway.timer
		echo 'OnStartupSec=20' >> /lib/systemd/system/dgidgateway.timer
		echo '' >> /lib/systemd/system/dgidgateway.timer
		echo '[Install]' >> /lib/systemd/system/dgidgateway.timer
		echo 'WantedBy=multi-user.target' >> /lib/systemd/system/dgidgateway.timer

		# We are not auto-starting this daemon on purpose, we're not ready for that.
		systemctl daemon-reload

		# Add the DGIdGateway Config
		echo '[General]' > /etc/dgidgateway
		echo 'Callsign=M1ABC' >> /etc/dgidgateway
		echo 'Suffix=ND' >> /etc/dgidgateway
		echo 'Id=1234567' >> /etc/dgidgateway
		echo 'RptAddress=127.0.0.1' >> /etc/dgidgateway
		echo 'RptPort=3200' >> /etc/dgidgateway
		echo 'LocalAddress=127.0.0.1' >> /etc/dgidgateway
		echo 'LocalPort=4200' >> /etc/dgidgateway
		echo 'RFHangTime=20' >> /etc/dgidgateway
		echo 'NetHangTime=20' >> /etc/dgidgateway
		echo 'Bleep=1' >> /etc/dgidgateway
		echo 'Debug=0' >> /etc/dgidgateway
		echo 'Daemon=1' >> /etc/dgidgateway
		echo '' >> /etc/dgidgateway
		echo '[Info]' >> /etc/dgidgateway
		echo 'RXFrequency=430000000' >> /etc/dgidgateway
		echo 'TXFrequency=430000000' >> /etc/dgidgateway
		echo 'Power=1' >> /etc/dgidgateway
		echo 'Latitude=50.0' >> /etc/dgidgateway
		echo 'Longitude=-3.0' >> /etc/dgidgateway
		echo 'Height=0' >> /etc/dgidgateway
		echo 'Description=Pi-Star' >> /etc/dgidgateway
		echo '' >> /etc/dgidgateway
		echo '[Log]' >> /etc/dgidgateway
		echo 'DisplayLevel=1' >> /etc/dgidgateway
		echo 'FileLevel=1' >> /etc/dgidgateway
		echo 'FilePath=/var/log/pi-star' >> /etc/dgidgateway
		echo 'FileRoot=DGIdGateway' >> /etc/dgidgateway
		echo 'FileRotate=0' >> /etc/dgidgateway
		echo '' >> /etc/dgidgateway
		echo '[APRS]' >> /etc/dgidgateway
		echo 'Enable=0' >> /etc/dgidgateway
		echo 'Address=127.0.0.1' >> /etc/dgidgateway
		echo 'Port=8673' >> /etc/dgidgateway
		echo 'Description=APRS Description' >> /etc/dgidgateway
		echo 'Suffix=Y' >> /etc/dgidgateway
		echo '' >> /etc/dgidgateway
		echo '[YSF Network]' >> /etc/dgidgateway
		echo 'Hosts=/usr/local/etc/YSFHosts.txt' >> /etc/dgidgateway
		echo 'RFHangTime=20' >> /etc/dgidgateway
		echo 'NetHangTime=20' >> /etc/dgidgateway
		echo 'Debug=0' >> /etc/dgidgateway
		echo '' >> /etc/dgidgateway
		echo '[FCS Network]' >> /etc/dgidgateway
		echo 'RFHangTime=20' >> /etc/dgidgateway
		echo 'NetHangTime=20' >> /etc/dgidgateway
		echo 'Debug=0' >> /etc/dgidgateway
		echo '' >> /etc/dgidgateway
		echo '[IMRS Network]' >> /etc/dgidgateway
		echo 'RFHangTime=20' >> /etc/dgidgateway
		echo 'NetHangTime=20' >> /etc/dgidgateway
		echo 'Debug=0' >> /etc/dgidgateway
		echo '' >> /etc/dgidgateway
		echo '[DGId=0]' >> /etc/dgidgateway
		echo 'Type=Gateway' >> /etc/dgidgateway
		echo 'Static=1' >> /etc/dgidgateway
		echo 'Address=127.0.0.1' >> /etc/dgidgateway
		echo 'Port=42025' >> /etc/dgidgateway
		echo 'Local=42026' >> /etc/dgidgateway
		echo 'RFHangTime=120' >> /etc/dgidgateway
		echo 'NetHangTime=60' >> /etc/dgidgateway
		echo 'Debug=0' >> /etc/dgidgateway
		echo '' >> /etc/dgidgateway
		echo '[GPSD]' >> /etc/dgidgateway
		echo 'Enable=0' >> /etc/dgidgateway
		echo 'Address=127.0.0.1' >> /etc/dgidgateway
		echo 'Port=2947' >> /etc/dgidgateway
		echo '' >> /etc/dgidgateway
		echo '[Enabled]' >> /etc/dgidgateway
		echo 'Enabled=0' >> /etc/dgidgateway

		# OK we are done
		Message="Upgraded from ${pistarCurVersion} to 4.1.6..."
		echo $Message
		exec "$0" "$@"
                exit 1
	;;
	4.1.6)
		#sed -i 's/4.1.6/4.1.7/g' /etc/pistar-release

		# Re-create the MOTD
		#/usr/local/sbin/pistar-motdgen

		# OK we are done
		#Message="Upgraded from ${pistarCurVersion} to 4.1.7..."
		Message="You are already running the latest version..."
		#echo $Message
		#exec "$0" "$@"
                #exit 1
	;;
	*)
		Message="Somthing went very wrong, I cant tell what version I am running"
	;;
	esac

	echo $Message
	echo "Upgrade process complete, syncing disk cache before making the disk Read-Only"
	# Make the disk read-only
	/bin/sync
	/bin/sync
	/bin/sync
	mount -o remount,ro /
        mount -o remount,ro /boot
	echo "Finished"

	}

if [ -t 1 ]; then
  # run via terminal, only output to screen
  main_function
else
  # if not run via terminal, log everything into a log file
  main_function >> /var/log/pi-star/pi-star_upgrade.log 2>&1
fi

exit 0
